import {
  Component,
  ChangeDetectionStrategy,
  OnInit
} from '@angular/core';
import { _HttpClient } from '@delon/theme';
import { NzMessageService } from 'ng-zorro-antd';
@Component({
  selector: 'system-setting',
  templateUrl: './setting.component.html',
  styleUrls: ['./setting.component.less'],
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class SystemSettingComponent implements OnInit {
  loading = true;
  setting: any;
  object: any;
  days = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28]
  constructor(
    private http: _HttpClient,
    private msg: NzMessageService
  ) {}
  ngOnInit() {
    this.http.get('api/setting/list').subscribe((res: any) => {
      this.object = res;
      this.loading = false;
      if(this.object) {
        this.setting = {
          isCheck: this.object.isCheck,
          checkDay: this.object.checkDay,
          isSalary: this.object.isSalary,
          salaryDay: this.object.salaryDay,
          isContract: this.object.isContract,
          isProbation: this.object.isProbation,
          staffBirthday: this.object.staffBirthday,
          companyBirthday: this.object.companyBirthday
        }
      }else{
        this.setting = {
          isCheck: false,
          checkDay: '1',
          isSalary: false,
          salaryDay: '10',
          isContract: false,
          isProbation: false,
          staffBirthday: false,
          companyBirthday: false
        }
      }
    });
  }
  save() {
    let obj = this.setting;
    if(this.object&&this.object._id){
      obj['_id'] = this.object._id;
    }
    this.http.patch('api/setting/update', obj).subscribe((res: any) => {
      if(res.success) {
        this.msg.success(`修改成功`);
      }else{
        this.msg.error(`修改失败`);
      }
    }, () => {
      this.loading = false;
      this.msg.error(`网络请求失败`);
    });
  }
}
